package sparseArray;

import org.junit.Test;

import java.io.*;
import java.util.Random;

public class SparseArray {
    int[][] sparseMatrix;//稀疏矩阵
    StringBuilder stringBuilder=new StringBuilder();//缓存文件
    int[][] ints;//原来的二维矩阵
    @Test
    public void test() throws IOException {
        //将普通矩阵转为稀疏矩阵
        sparseArrayChange();
        //将稀疏矩阵写到磁盘
        writeArray(sparseMatrix);
        //从磁盘读取稀疏矩阵
        stringBuilder=readArray();
        System.out.println("读取到的稀疏矩阵--------------------");
        System.out.println(stringBuilder.toString());
        //将稀疏矩阵还原为二维矩阵
        int[][] ints = changeSparseToCommon(stringBuilder);

        for (int i = 0; i < ints.length; i++) {
            for (int j = 0; j < ints[i].length; j++) {
                System.out.print(ints[i][j]+"  ");
            }
            System.out.println("");
        }

    }

public int[][] changeSparseToCommon(StringBuilder stringBuilder){
    String[] strings=new String[18];
    String string= stringBuilder.toString();
    strings= string.split("/");
    int[][] ints = new int[Integer.parseInt(strings[0])][Integer.parseInt(strings[1])];
    //还原二维数组
    for (int i=3;i<strings.length-2;i+=3){
        ints[Integer.parseInt(strings[i])][Integer.parseInt(strings[i+1])]=Integer.parseInt(strings[i+2]);
    }
    //写完代码该返回值
        return ints;
}



    @Test
    public void sparseArrayChange(){
        int[][] ints = new int[11][11];
//         加入五个数据

        for (int i=1;i<=5;i++){
            Random random = new Random();
            int rad = random.nextInt(10);
            int rad1=random.nextInt(10);
             while(ints[rad][rad1]>0){
                  rad = random.nextInt(10);
                  rad1=random.nextInt(10);
             }
               ints[rad][rad1]=i;
        }

        int datanum=0;
        //遍历查看原型举证
        System.out.println("初始数组------------------------------");
        for (int i = 0; i < ints.length; i++) {
            for (int j = 0; j < ints[i].length; j++) {
                if(ints[i][j]!=0)
                    datanum++;
                System.out.print(ints[i][j]+"  ");
            }
            System.out.println("");
        }
        System.out.println("有效数据个数:"+datanum);
       //将原始二维矩阵转换为稀疏矩阵
        sparseMatrix = new int[datanum + 1][3];
         //稀疏矩阵的第一行记录原来二维矩阵的行列和有效数据
            //总行数
        sparseMatrix[0][0]=ints.length;
            //总列数
        sparseMatrix[0][1]=ints[1].length;
           //有效数据个数
        sparseMatrix[0][2]=datanum;
        //遍历原始数组建立稀疏数组
        int noData=1;
        for (int i = 0; i < ints.length; i++) {
            for (int j = 0; j < ints[i].length; j++) {
                if (ints[i][j]!=0)
                {
                    sparseMatrix[noData][0]=i;
                    sparseMatrix[noData][1]=j;
                    sparseMatrix[noData][2]=ints[i][j];
                    noData++;
                }
            }
        }

        //输出稀疏数组
        System.out.println("稀疏数组------------------------------------");
        for (int i = 0; i < sparseMatrix.length; i++) {
            for (int j = 0; j < sparseMatrix[i].length; j++) {
                if (j==0)
                System.out.print("["+i+"]");
                System.out.print(sparseMatrix[i][j]+"  ");
            }
            System.out.println(" ");
        }
    }
    private <T> StringBuilder readArray() throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new FileReader(new File("I:/稀疏数组.txt")));
        StringBuilder builder = new StringBuilder();
        String buf;
       while ((buf=bufferedReader.readLine())!=null){
           builder.append(buf);
       }
        //记得改返回值
        return builder;
    }
    private <T> void writeArray(int[][] arr) throws IOException {
        FileWriter writer = new FileWriter(new File("i:/稀疏数组.txt"));
        for (int[] a : arr) {
            for (int data:a) {
               writer.write(+data+"/");
            }
            writer.write("\n");
        }
        writer.flush();
        writer.close();
    }
            
}
